﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CMS.ORM;//请先添加引用
namespace CMS.ORM.DAL
{
    /// <summary>
    /// 数据访问类Vote。
    /// </summary>
    public class Vote
    {
        public Vote()
        { }
        #region  成员方法
        /// <summary>
        /// 得到最大ID
        /// </summary>
        /// <returns></returns>
        public decimal GetMaxId()
        {
            return DbHelperSQL.GetMaxIDDecimal("id", "Vote");
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Vote");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CMS.ORM.Model.Vote model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Vote(");
            strSql.Append("id,title,description,option1,option2,option3,option4,option5,maxOption,maxSelect,creator,createTime,lastUpdater,lastUpdateTime,deadline)");
            strSql.Append(" values (");
            strSql.Append("@id,@title,@description,@option1,@option2,@option3,@option4,@option5,@maxOption,@maxSelect,@creator,@createTime,@lastUpdater,@lastUpdateTime,@deadline)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal,9),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,500),
					new SqlParameter("@option1", SqlDbType.NVarChar,50),
					new SqlParameter("@option2", SqlDbType.NVarChar,50),
					new SqlParameter("@option3", SqlDbType.NVarChar,50),
					new SqlParameter("@option4", SqlDbType.NVarChar,50),
					new SqlParameter("@option5", SqlDbType.NVarChar,50),
					new SqlParameter("@maxOption", SqlDbType.SmallInt,2),
					new SqlParameter("@maxSelect", SqlDbType.SmallInt,2),
					new SqlParameter("@creator", SqlDbType.NVarChar,50),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@lastUpdater", SqlDbType.NVarChar,50),
					new SqlParameter("@lastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@deadline", SqlDbType.DateTime)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.title;
            parameters[2].Value = model.description;
            parameters[3].Value = model.option1;
            parameters[4].Value = model.option2;
            parameters[5].Value = model.option3;
            parameters[6].Value = model.option4;
            parameters[7].Value = model.option5;
            parameters[8].Value = model.maxOption;
            parameters[9].Value = model.maxSelect;
            parameters[10].Value = model.creator;
            parameters[11].Value = model.createTime;
            parameters[12].Value = model.lastUpdater;
            parameters[13].Value = model.lastUpdateTime;
            parameters[14].Value = model.deadline;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CMS.ORM.Model.Vote model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Vote set ");
            strSql.Append("title=@title,");
            strSql.Append("description=@description,");
            strSql.Append("option1=@option1,");
            strSql.Append("option2=@option2,");
            strSql.Append("option3=@option3,");
            strSql.Append("option4=@option4,");
            strSql.Append("option5=@option5,");
            strSql.Append("maxOption=@maxOption,");
            strSql.Append("maxSelect=@maxSelect,");
            strSql.Append("creator=@creator,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("lastUpdater=@lastUpdater,");
            strSql.Append("lastUpdateTime=@lastUpdateTime,");
            strSql.Append("deadline=@deadline");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal,9),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,500),
					new SqlParameter("@option1", SqlDbType.NVarChar,50),
					new SqlParameter("@option2", SqlDbType.NVarChar,50),
					new SqlParameter("@option3", SqlDbType.NVarChar,50),
					new SqlParameter("@option4", SqlDbType.NVarChar,50),
					new SqlParameter("@option5", SqlDbType.NVarChar,50),
					new SqlParameter("@maxOption", SqlDbType.SmallInt,2),
					new SqlParameter("@maxSelect", SqlDbType.SmallInt,2),
					new SqlParameter("@creator", SqlDbType.NVarChar,50),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@lastUpdater", SqlDbType.NVarChar,50),
					new SqlParameter("@lastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@deadline", SqlDbType.DateTime)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.title;
            parameters[2].Value = model.description;
            parameters[3].Value = model.option1;
            parameters[4].Value = model.option2;
            parameters[5].Value = model.option3;
            parameters[6].Value = model.option4;
            parameters[7].Value = model.option5;
            parameters[8].Value = model.maxOption;
            parameters[9].Value = model.maxSelect;
            parameters[10].Value = model.creator;
            parameters[11].Value = model.createTime;
            parameters[12].Value = model.lastUpdater;
            parameters[13].Value = model.lastUpdateTime;
            parameters[14].Value = model.deadline;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Vote ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS.ORM.Model.Vote GetModel(decimal id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,title,description,option1,option2,option3,option4,option5,maxOption,maxSelect,creator,createTime,lastUpdater,lastUpdateTime,deadline from Vote ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            CMS.ORM.Model.Vote model = new CMS.ORM.Model.Vote();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = decimal.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.title = ds.Tables[0].Rows[0]["title"].ToString();
                model.description = ds.Tables[0].Rows[0]["description"].ToString();
                model.option1 = ds.Tables[0].Rows[0]["option1"].ToString();
                model.option2 = ds.Tables[0].Rows[0]["option2"].ToString();
                model.option3 = ds.Tables[0].Rows[0]["option3"].ToString();
                model.option4 = ds.Tables[0].Rows[0]["option4"].ToString();
                model.option5 = ds.Tables[0].Rows[0]["option5"].ToString();
                if (ds.Tables[0].Rows[0]["maxOption"].ToString() != "")
                {
                    model.maxOption = int.Parse(ds.Tables[0].Rows[0]["maxOption"].ToString());
                }
                if (ds.Tables[0].Rows[0]["maxSelect"].ToString() != "")
                {
                    model.maxSelect = int.Parse(ds.Tables[0].Rows[0]["maxSelect"].ToString());
                }
                model.creator = ds.Tables[0].Rows[0]["creator"].ToString();
                if (ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                model.lastUpdater = ds.Tables[0].Rows[0]["lastUpdater"].ToString();
                if (ds.Tables[0].Rows[0]["lastUpdateTime"].ToString() != "")
                {
                    model.lastUpdateTime = DateTime.Parse(ds.Tables[0].Rows[0]["lastUpdateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["deadline"].ToString() != "")
                {
                    model.deadline = DateTime.Parse(ds.Tables[0].Rows[0]["deadline"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,title,description,option1,option2,option3,option4,option5,maxOption,maxSelect,creator,createTime,lastUpdater,lastUpdateTime,deadline ");
            strSql.Append(" FROM Vote ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,title,description,option1,option2,option3,option4,option5,maxOption,maxSelect,creator,createTime,lastUpdater,lastUpdateTime,deadline ");
            strSql.Append(" FROM Vote ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Vote";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}


